package com.coollang.smartbra.choosephoto;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.coollang.smartbra.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 
 * 作者：陈冬冬
 * 
 * 说明：从相册选择照片
 * 
 * 时间：2015-10-30 下午4:37:09
 * 
 */
public class ImageChooseActivity_new extends Activity implements OnClickListener {

	private List<ImageItem> mDataList;
	private ImageFetcher mHelper;
	private GridView mGridView;
	private TextView mBucketNameTv;
	private ImageGridAdapter_new mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_choose);
		mDataList = new ArrayList<>();

		mHelper = ImageFetcher.getInstance(getApplicationContext());
		mDataList = mHelper.getDateImageList();

		initview();
	}

	private void initview() {
		mBucketNameTv = (TextView) findViewById(R.id.title);
		mBucketNameTv.setText(getString( R.string.photo));

		Button btn_finish = (Button) findViewById(R.id.finish_btn);
		btn_finish.setVisibility(View.GONE);
		TextView tv_cancel = (TextView) findViewById(R.id.action);
		tv_cancel.setVisibility(View.VISIBLE);
		tv_cancel.setOnClickListener(this);
		ImageView iv_return = (ImageView) findViewById(R.id.iv_return);
		iv_return.setVisibility(View.VISIBLE);
		iv_return.setOnClickListener(this);
		
		mGridView = (GridView) findViewById(R.id.gridview);
		mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mAdapter = new ImageGridAdapter_new(ImageChooseActivity_new.this, mDataList);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Intent data = new Intent();
				data.putExtra(IntentConstants.PICTUREURI, mDataList.get(position).sourcePath);
				setResult(RESULT_OK, data);
				finish();
				overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.action: //取消
			finish();
			break;

		case R.id.iv_return: //返回
			finish();
			break;
		}
	}
}
